using UnityEngine;
using System.Collections;

[System.Serializable]
public class Puzzle1 : MonoBehaviour {
	
	/**
	 *	x = (n*bp)+(x+1) 
	 **/
	public static int x = 0; // Value
	public static int bp = 0; // Button presses
	
	public static int valueGet = 15;
	public static bool[] buttonsClicked = new bool[] {false, false, false, false, false};
	
	public static Puzzle1 puzzle1;
	
	public GameObject doorToOpen;
	
	public string turnOnLightsWithTag = "";
		
	private GameObject[] lightsToTurnOn;
	
	void Start() {
		puzzle1 = this;
	}
	
	public void openDoor() {
		doorToOpen.animation.Play();
		doorToOpen.audio.PlayOneShot(doorToOpen.audio.clip);
	}
	
	public void turnOnLights() {
		lightsToTurnOn = GameObject.FindGameObjectsWithTag(turnOnLightsWithTag);
		// Make sure all lights are on:
		foreach (GameObject l in lightsToTurnOn) {
			// Only light objects:
			if (l.light != null) {
				l.light.enabled = true;
			}
			
			if (l.particleSystem != null) {
				l.particleSystem.Stop();
			}
		}
	}
	
	/**
	 * n = button
	 **/
	public static int doButtonPress(int n) {
		if (!buttonsClicked[n]) {
			buttonsClicked[n] = true;
			x = ((n+1) * bp) + (x +1 );
			bp++;
		}
		if (x > 15) {
			Debug.Log("Puzzle failed.");
			if (Game.game.difficulty == Game.DIFFICULTY_EASY) {
				Puzzle1.resetPuzzle();	
			}
		} else if (x == 15) {
			puzzle1.openDoor();
			puzzle1.turnOnLights();
		} else {
			bool allClicked = true;
			foreach (bool b in buttonsClicked) {
				if (!b) {
					allClicked = false;	
				}
			}
			
			if (allClicked && Game.game.difficulty == Game.DIFFICULTY_EASY) {
				resetPuzzle();	
			}
		}
		
		Debug.Log("x is: " + x);
		
		return x;
	}
	
	public static void resetPuzzle() {
		x = 0;
		bp = 0;
	}
}
